// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import VCharts from 'v-charts'
import 'element-ui/lib/theme-chalk/index.css'
import './assets/css/global.css'
import Moment from 'moment'
import Vuex from 'vuex'
import store from './vuex/store'
import api from '@/api/index'
import Axios from 'axios'
import has from './utils/hasPermission'

Vue.use(Vuex)
Vue.use(ElementUI)
Vue.use(VCharts)
Vue.use(has)
Vue.prototype.$axios = Axios

// 定义中文 Moment 时间过滤器
Moment.locale('zh-cn')
Vue.filter('convertTime', function (data, formatStr) {
  return Moment(data).format(formatStr)
})
Vue.filter('relativeTime', function (data) {
  return Moment(data).fromNow()
})

Vue.config.productionTip = false

// 全局挂载api
Vue.prototype.$apis = api

// 服务端口指定
Vue.prototype.$repairPort = 8701
Vue.prototype.$commonPort = 8702

router.beforeEach((to, from, next) => {
  if (to.path === '/') {
    next({ path: '/Login' })
  } else if (to.path === '/Login' || to.path === '/Register') {
    localStorage.removeItem('access-token')
    next()
  } else {
    const token = localStorage.getItem('access-token')
    // console.log('token:==============', token)
    if (!token) {
      next({ path: '/Login' })
    } else {
      if (to.path === '/Index') {
        next({ path: '/Dashboard' })
      }
      next()
    }
  }
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
